//
//  NoAttentionTableViewCell.m
//  SouthChina
//
//  Created by Mr.Y on 14/11/6.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "PublicAccountCell.h"
#import "MessageSessionDao.h"

@interface PublicAccountCell ()
{
    
}

@end

@implementation PublicAccountCell

- (void)awakeFromNib {
    // Initialization code
    _badgerNumer.layer.masksToBounds = YES;
    _badgerNumer.layer.cornerRadius = _badgerNumer.frame.size.height/2;

}
- (IBAction)didTapButton:(id)sender {
    if ([self.delegate respondsToSelector:@selector(publicAccountCell:didTapButton:)])
    {
        [self.delegate performSelector:@selector(publicAccountCell:didTapButton:) withObject:self   withObject:sender];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setPublicAccountCell:(PublicAccount*)publicaccount
{
    _publicAccount = publicaccount;
    //设置应用图片
    NSURL *url = [NSURL URLWithString: publicaccount.headImg];
    [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    
    CGSize appNameContentSize = [publicaccount.appName sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(180, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
    self.titleLabel.frame = CGRectMake(60, 6, appNameContentSize.width, 21);
    UILabel * officeLabel = [[UILabel alloc] init];
    UILabel * debugLabel = [[UILabel alloc] init];
    
    BOOL isDebug =[@"1" isEqualToString:[publicaccount getValue:@"debugMode"] ];
    BOOL isOffice =[@"1" isEqualToString:[publicaccount getValue:@"isOfficial"]];
    if (isOffice) {
        /*officeLabel.frame = CGRectMake(8+self.titleLabel.frame.size.width+self.titleLabel.frame.origin.x,self.titleLabel.frame.origin.y,30, 18);
        officeLabel.text = @"官方";
        officeLabel.textColor = [UIColor whiteColor];
        
        officeLabel.font = [UIFont boldSystemFontOfSize:12.0f];
        officeLabel.textAlignment = NSTextAlignmentCenter;
        officeLabel.backgroundColor = [UIColor colorWithRed:0/255.0f green:128/255.0f blue:0/255.0f alpha:1];
        [self addSubview:officeLabel];*/
       UIImageView * officalImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.headImage.frame.origin.x-8, self.headImage.frame.origin.y-8,self.headImage.frame.size.width,self.headImage.frame.size.height)];
        officalImage.image = [UIImage imageNamed:@"offcial.png"];
        [self.headImage addSubview:officalImage];
    }else if (isDebug)
    {
        debugLabel.frame = CGRectMake(8+self.titleLabel.frame.size.width+self.titleLabel.frame.origin.x+officeLabel.frame.size.width+officeLabel.frame.origin.x,6,50, 18);
        debugLabel.text = @"调试中";
        debugLabel.textColor = [UIColor whiteColor];
        debugLabel.font = [UIFont boldSystemFontOfSize:12.0f];
        debugLabel.textAlignment = NSTextAlignmentCenter;
        debugLabel.backgroundColor = [UIColor colorWithRed:178/255.0f green:34/255.0f blue:34/255.0f alpha:1];
        [self addSubview:debugLabel];
    }
    self.titleLabel.text = publicaccount.appName;
    self.detailLabel.text = publicaccount.funcDesc;
    
    //设置提醒数
    NSString *sessonid = [NSString stringWithFormat:@"%@_%@",MESSAGE_TYPE_P2P,publicaccount.appId];
    MessageSession *appSession = [MessageSessionDao queryMsgSessionById:sessonid];
    
    NSString *badgeValue =@"0";
    if (appSession) {
        badgeValue = [NSString stringWithFormat:@"%d",appSession.badgeNumber];
        if ([publicaccount.appName isEqualToString:@"图书馆"]) {
            NSLog(@"--------消息数%@",badgeValue);
        }
    }
    [self setBadgeValue:badgeValue];    
}

- (void)setBadgeValue:(NSString *)badgeValue
{
    
    if (badgeValue && badgeValue.doubleValue > 0)
    {
        _badgerNumer.hidden = NO;
        
        if (badgeValue.doubleValue > 99)
        {
            _badgerNumer.text = @"99+";
        }
    }
    else
    {
        _badgerNumer.hidden = YES;
    }
    
    _badgerNumer.text = badgeValue;
}
@end
